import java.util.*;
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private Food bag = null;
    private Food princess = null;
    
    private int aggressivity = 10;
    private int Defense = 10;
    
    public int strength = 100;
    
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, fristfloor, secondfloor, thirdfloor, fourthfloor, fifthfloor, sixthfloor, trap, towertop;
        
        // create the rooms
        outside = new Room("魔塔外");
        fristfloor = new Room("魔塔的第一层，竟然没有怪物在这里，可以试着寻找一下物品", new Food("苹果 [能量：10]", 10));
        secondfloor = new Room("魔塔的第二层，你打败了一只史莱姆，看看获得了什么", new Food("生锈的盔甲 [防御力：5]", 10));
        thirdfloor = new Room("魔塔的第三层，经历了一场鏖战，获得了胜利");
        fourthfloor = new Room("魔塔的第四层，打败了红衣法师，从他身上好像掉落了什么", new Food("附魔的铁剑[攻击力：20]", 10));
        fifthfloor = new Room("魔塔的第五层，打败了骷髅战士，看看获得了什么", new Food("附魔的盔甲[防御力：20]", 10));
        sixthfloor = new Room("魔塔的第六层,在这里你看到了被囚禁的公主，可输入save解救",new Food("公主[生命：100]", 0));
        trap = new Room("布满陷阱的房间，身受重伤，请重新再来！");
        towertop = new Room("塔顶");
        
        // initialise room exits
        outside.setExit("up", fristfloor);
        fristfloor.setExit("west", secondfloor);
        fristfloor.setExit("east", thirdfloor);
        fristfloor.setExit("north", trap);
        secondfloor.setExit("up", fourthfloor);
        secondfloor.setExit("east", fristfloor);
        thirdfloor.setExit("up", fifthfloor);
        thirdfloor.setExit("west", fristfloor);
        fourthfloor.setExit("east", sixthfloor);
        fourthfloor.setExit("down", secondfloor);
        fifthfloor.setExit("west", sixthfloor);
        fifthfloor.setExit("down", fourthfloor);
        sixthfloor.setExit("up", towertop);
        sixthfloor.setExit("west", fourthfloor);
        sixthfloor.setExit("east", fifthfloor);
        
        currentRoom = outside;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("感谢您的游玩，再见");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println("勇士听说国王的女儿被一群怪物抓走了，那群怪物就位于一座魔塔当中，很多人去营救公主，但都没人能够回来。勇士决定去营救公主，于是来到了魔塔");
        System.out.println("欢迎来到魔塔,勇士");
        System.out.println("你的任务是到达魔塔的顶层，打败魔王并救出王国的公主");
        System.out.println("当然还要小心沿途的怪物与陷阱");
        System.out.println("你可以输入help以获得帮助");
        System.out.println();
        System.out.println("你现在处于" + currentRoom.getDescription());
        currentRoom.printExits();
    }
    
    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("抱歉，我不知道你的意思");
            return false;
        }
        
        Word commandWord = command.getCommandWord();
        switch(commandWord) {
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case STATE:
                System.out.println("体力："+strength+"，攻击力："+aggressivity+"，防御力："+Defense);
            case LOOK:
                Food food = currentRoom.getFood();
                if(food != null) {
                    System.out.println("房间里有" + food.getName());
                }
                else {
                    System.out.println("这个房间里什么也没有。");
                }
                break;
            case PICK:
                Food food1 = currentRoom.getFood();
                if(food1.getName()=="苹果 [能量：10]"){
                     if(bag==null){
                         bag=food1;
                         System.out.println("你捡起了"+ food1.getName());
                     }
                     else
                         System.out.println("你的背包满了");
                }
                else if(food1.getName()=="附魔的铁剑[攻击力：20]"){
                     aggressivity+=20;
                     System.out.println("你的攻击力增加了20");
                }
                else if(food1.getName()=="生锈的盔甲 [防御力：5]"){
                     Defense+=5;
                     System.out.println("你的防御力增加了5");
                }
                else if(food1.getName()=="附魔的盔甲[防御力：20]"){
                     Defense+=20;
                     System.out.println("你的防御力增加了20");
                }
                break;
            case SAVE:
                Food princess1 = currentRoom.getFood();
                if(princess1.getName()=="公主[生命：100]"){
                     if(princess == null){
                         princess = princess1;
                         System.out.println("你救出了"+ princess1.getName());
                     }
                }
                break;
            case EAT:
                if(bag != null){
                    System.out.println("你吃了" + bag.getName());
                    player.eat(bag.getName());
                    strength+=bag.getEnergy();
                    if(strength>100)
                    strength=100;
                    bag=null;
                }
                else{
                    System.out.println("你已经没有东西可以补充体力了。");
                }
                break;
            case CHECK:
                if(bag != null){
                    System.out.println("你的背包里有" + bag.getName());
                }
                else{
                    System.out.println("你的背包空空如也。");
                }
                if(princess != null){
                    System.out.println("你已经救出了公主。");
                }
                else{
                    System.out.println("你还没有救出公主。");
                }
                break;
        }
        if(princess != null && currentRoom.getDescription()=="塔顶"){
            System.out.println("你成功救出了公主，并打败了魔王，恭喜通关!");
            wantToQuit = true;
        }
        else if(currentRoom.getDescription()=="塔顶"&& princess == null){
            System.out.println("你打败了魔王，却没能找到公主，想要再来一次吗？哈哈哈哈");
            wantToQuit = true;
        }
        else if(currentRoom.getDescription()=="魔塔的第三层，经历了一场鏖战，获得了胜利"){
            strength -=15;
        }
        else if(currentRoom.getDescription()=="布满陷阱的房间，身受重伤，请重新再来！"){
            wantToQuit = true;
        }
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你可以输入以下指令");
        Word[] words = Word.values();
        for(int i = 0; i < words.length; i++) {
            System.out.print(words[i].getCommandWord()+" ");
        }
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪里?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom =currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("这里没有出口");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在处于" + currentRoom.getDescription());
            System.out.println("可输入state查看当前状态");
            currentRoom.printExits();
        }
    }
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("确认退出?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
